package com.brotherly.mpgp.widget;

import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONTokener;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.brotherly.mpgp.remoteinterface.RemoteServerWatcher;

public class GameQueryBroadcastReceiver extends BroadcastReceiver
{
    public static final int publishGame = 0;
    public static final int publishArticle = 1;
    private Object obj = new Object();

    @Override
    public void onReceive(final Context context, Intent intent)
    {
        try
        {
            String text = intent.getExtras().getString("text");
            String name = intent.getExtras().getString("name");
            Log.d("GameQueryBroadcastReceiver", "text:" + text);
            Log.d("GameQueryBroadcastReceiver", "name:" + name);
            JSONTokener jsonParser = new JSONTokener(text);
            JSONObject body = (JSONObject) jsonParser.nextValue();
            final String intents = body.getString("intent");
            String content = "";
            String imagePath = null;
            if ("com.brotherly.mpgp.MainActivity".equals(intents))
            {
                JSONObject data = body.getJSONObject("data");
                String type = body.getString("type");
                if ("org.strategy.games.notify".equals(type))
                {
                    String msg = data.getString("text");
                    String url = data.getString("url");
                    content = msg;
                } else
                {
                    JSONArray games = data.getJSONArray("games");

                    int count = games.length();
                    JSONObject game = games.getJSONObject(0);

                    if (count > 1)
                    {
                        StringBuilder bui = new StringBuilder("新增了").append(game.getString("name")).append("和另外")
                                .append(count - 1).append("个游戏");
                        content = bui.toString();
                    } else
                    {
                        StringBuilder bui = new StringBuilder("新增了").append(game.getString("name"));
                        content = bui.toString();
                    }

                    String identifier = game.getString("identifier");
                    int id = game.getInt("id");

                    imagePath = new StringBuffer(RemoteServerWatcher.RemoteServerRoot)
                            .append("games/blob?key=image&gameref=")
                            .append(id).toString();
                }
            }
            else if ("com.brotherly.mpgp.ArticleListActivity".equals(intents))
            {
                JSONArray games = body.getJSONArray("data");
                JSONObject game = games.getJSONObject(0);

                StringBuilder bui = new StringBuilder().append(game.getString("name")).append("攻略《")
                        .append(game.getString("title")).append("》");
                content = bui.toString();
                int id = game.getInt("id");
                imagePath = new StringBuffer(RemoteServerWatcher.RemoteServerRoot)
                        .append("games/blob?key=image&gameref=")
                        .append(id).toString();
            }

            Intent in = new Intent();
            in.putExtra("intents", intents);
            in.putExtra("content", content);
            if (imagePath != null)
            {
                in.putExtra("imagePath", imagePath);
            }
            in.setAction("android.appwidget.action.APPWIDGET_UPDATE");
            context.sendBroadcast(in);
        }
        catch (Exception e)
        {
            Log.e("GameQueryBroadcastReceiver", e.getMessage());
        }

    }
}
